<template>
  <div>
    <script :id="id" name="content" type="text/plain"></script>
  </div>
</template>

<script>
  import './editor.js'
  import './lang/zh-cn'
  import './ueditor.config'
  import './core/browser.js'
  import './core/utils.js'
  import './core/EventBase.js'
  import './core/dtd.js'
  import './core/domUtils.js'
  import './core/Range.js'
  import './core/Selection.js'
  import './core/Editor.js'
  import './core/Editor.defaultoptions.js'
  import './core/filterword.js'
  import './core/node.js'
  import './core/htmlparser.js'
  import './core/filternode.js'
  import './core/plugin.js'
  import './core/keymap.js'
  import './core/localstorage.js'
  import './plugins/defaultfilter.js'
  import './plugins/inserthtml.js'
  import './plugins/autotypeset.js'
  import './plugins/autosubmit.js'
  import './plugins/background.js'
  import './plugins/image.js'
  import './plugins/justify.js'
  import './plugins/font.js'
  import './plugins/link.js'
  import './plugins/iframe.js'
  import './plugins/removeformat.js'
  import './plugins/blockquote.js'
  import './plugins/convertcase.js'
  import './plugins/indent.js'
  import './plugins/print.js'
  import './plugins/preview.js'
  import './plugins/selectall.js'
  import './plugins/paragraph.js'
  import './plugins/directionality.js'
  import './plugins/horizontal.js'
  import './plugins/time.js'
  import './plugins/rowspacing.js'
  import './plugins/lineheight.js'
  import './plugins/cleardoc.js'
  import './plugins/anchor.js'
  import './plugins/wordcount.js'
  import './plugins/pagebreak.js'
  import './plugins/dragdrop.js'
  import './plugins/undo.js'
  import './plugins/copy.js'
  import './plugins/paste.js'
  import './plugins/puretxtpaste.js'
  import './plugins/list.js'
  import './plugins/source.js'
  import './plugins/enterkey.js'
  import './plugins/keystrokes.js'
  import './plugins/fiximgclick.js'
  import './plugins/upimgmove.js'
  import './plugins/autolink.js'
  import './plugins/autoheight.js'
  import './plugins/autofloat.js'
  // import './plugins/video.js'
  import './plugins/table.core.js'
  import './plugins/table.cmds.js'
  import './plugins/table.action.js'
  import './plugins/table.sort.js'
  import './plugins/contextmenu.js'
  import './plugins/shortcutmenu.js'
  import './plugins/basestyle.js'
  import './plugins/elementpath.js'
  import './plugins/formatmatch.js'
  import './plugins/searchreplace.js'
  import './plugins/customstyle.js'
  import './plugins/catchremoteimage.js'
  import './plugins/insertparagraph.js'
  import './plugins/template.js'
  // import './plugins/music.js'
  import './plugins/autoupload.js'
  import './plugins/autosave.js'
  import './plugins/section.js'
  // import './plugins/simpleupload.js'
  import './plugins/serverparam.js'
  import './plugins/xssFilter.js'
  import './ui/ui.js'
  import './ui/uiutils.js'
  import './ui/uibase.js'
  import './ui/separator.js'
  import './ui/mask.js'
  import './ui/popup.js'
  import './ui/colorpicker.js'
  import './ui/tablepicker.js'
  import './ui/stateful.js'
  import './ui/button.js'
  import './ui/splitbutton.js'
  import './ui/colorbutton.js'
  import './ui/tablebutton.js'
  import './ui/autotypesetpicker.js'
  import './ui/autotypesetbutton.js'
  import './ui/cellalignpicker.js'
  import './ui/pastepicker.js'
  import './ui/toolbar.js'
  import './ui/menu.js'
  import './ui/combox.js'
  import './ui/dialog.js'
  import './ui/menubutton.js'
  import './ui/multiMenu.js'
  import './ui/shortcutmenu.js'
  import './ui/breakline.js'
  import './ui/message.js'
  import './adapter/editorui.js'
  import './adapter/editor.js'
  import './adapter/message.js'
  import './adapter/autosave.js'

  import su from './mixins/simpleupload'
  export default {
    name: 'zz-ueditor',
    mixins: [su],
    data () {
      return {
        instance: null,
        imgSrcReg: /<img [^>]*src=['"]([^'"]+)[^>]*>/gi,
        config: {
          initialFrameHeight: 500
        }
      }
    },
    mounted () {
      if (this.isDirect) {
        this.initEditor()
      }
    },
    props: {
      upload: {
        type: Object,
        default: function () {
          return {
            serverUrl: '/common/upload.htm',
            serverLabel: 'detail'
          }
        }
      },
      id: {
        type: String,
        default: 'container'
      },
      isDirect: {
        type: Boolean,
        default: true
      },
      subSystem: {
        type: Number,
        default: 2 // 上传方 1：CCBIM 2：公司内部 3 第三方
      },
      fileOwnerType: {
        type: Number,
        default: 2 // 上传人 1：个人 2：企业
      },
      toolbars: {
        type: Array,
        default: function () {
          return [[
            'fullscreen', 'source', '|', 'undo', 'redo', '|',
            'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
            'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
            'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
            'directionalityltr', 'directionalityrtl', 'indent', '|',
            'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
            'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
            'simpleupload', 'pagebreak', 'template', 'background', '|',
            'horizontal', 'date', 'time', 'spechars', '|',
            'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', '|',
            'print', 'preview', 'searchreplace', 'drafts'
          ]]
        }
      },
      initialFrameHeight: {
        type: Number,
        default: 500
      }
    },
    beforeDestroy () {
      this.instance.destroy()
    },
    methods: {
      initEditor () {
        this.$nextTick(() => {
          this.config.toolbars = this.toolbars
          this.config.initialFrameHeight = this.initialFrameHeight
          this.config = {...this.config, ...this.upload}
          this.instance = window.UE.getEditor(this.id, this.config)
          // 绑定事件，当 UEditor 初始化完成后，将编辑器实例通过自定义的 ready 事件交出去
          this.instance.addListener('ready', () => {
            this.$emit('ready', this.instance)
            window.UE.vue = this
          })
        })
      },
      setContent (content = '') {
        if (!content) {
          content = ''
        }
        this.instance.setContent(content)
      },
      setNoFilterContent (content = '') {
        if (!content) {
          content = ''
        }
        this.instance.setNoFilterContent(content)
      },
      getContent () {
        return this.instance.getContent()
      },
      dropImgSrc (reg, str) {
        str.replace(reg, function (match, capture) {
          str = str.replace(capture, '')
        })
        return str
      }
    }
  }
</script>

